//
//  RegisterTwoVC.m
//  qwe
//
//  Created by 谢志振 on 16/8/12.
//  Copyright © 2016年 谢志振. All rights reserved.
//

#import "RegisterTwoVC.h"
#import "SDAutoLayout.h"
#import "VDJConstant.h"
@interface RegisterTwoVC ()
@property int totalTime;
@property (nonatomic,strong) NSTimer * timer;
@end
@implementation RegisterTwoVC
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self baseUI];

}
- (void)onTapCodeButton:(UIButton *)sender
{
    sender.enabled = NO;
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshCodeButton:) userInfo:sender repeats:YES];
    _timer = timer;
    _totalTime = 60;
    
}
- (void)onTapRegistButton
{
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}
- (void)refreshCodeButton:(NSTimer *)timer
{
    UIButton * btn = timer.userInfo;
    [btn setTitle:[NSString stringWithFormat:@"%d秒重新获取",--_totalTime] forState:0];
    if (_totalTime == 0) {
        [btn setTitle:@"获取手机验证码" forState:0];
        btn.enabled = YES;
        if (_timer) {
            [_timer invalidate];
            _timer = nil;
        }
        
    }
}
- (void)baseUI
{
    
    UIImageView * bg = [[UIImageView alloc]initWithFrame:self.view.bounds];
    bg.image = [UIImage imageNamed:@"登录背景"];
    [self.view addSubview:bg];
    
    NSArray * arr = @[@"姓名:",@"密码:",@"确认密码:",@"手机验证码:"];
    UIView * referenceView = self.view;
    CGFloat margin;
    for (int i = 0; i < 4; i ++) {
        if (i == 0) {
            margin = self.view.frame.size.height/4;
        }else{
            margin = 30 + 15 + 2 * 8;
        }
        UILabel * name = [UILabel new];
        name.textColor = [UIColor whiteColor];
        name.font = [UIFont systemFontOfSize:12];
        name.text = arr[i];
        [self.view addSubview:name];
        
        UILabel * horizontalLine = [UILabel new];
        horizontalLine.backgroundColor = [UIColor whiteColor];
        horizontalLine.tag = 100+ i;
        [self.view addSubview:horizontalLine];
        
        UITextField * textField = [UITextField new];
        textField.borderStyle = UITextBorderStyleNone;
        textField.textColor = [UIColor whiteColor];
        textField.font = [UIFont systemFontOfSize:12];
        textField.tag = i + 1;
        [self.view addSubview:textField];
        
        horizontalLine.sd_layout.centerXEqualToView(self.view).heightIs(1).widthRatioToView(self.view,0.6f).topSpaceToView(referenceView,margin);
        name.sd_layout.leftEqualToView(horizontalLine).bottomSpaceToView(horizontalLine,8).heightIs(15);
        [name setSingleLineAutoResizeWithMaxWidth:150];
        textField.sd_layout.leftSpaceToView(name,5).rightEqualToView(horizontalLine).centerYEqualToView(name).heightIs(20);
        
        referenceView = horizontalLine;
    }
    
    UILabel * label = (UILabel*)[self.view viewWithTag:103];
    //验证码
    UIButton * verificationCode = [UIButton new];
    [verificationCode setTitle:@"获取手机验证码" forState:0];
    verificationCode.titleLabel.font = [UIFont systemFontOfSize:10];
    [verificationCode addTarget:self action:@selector(onTapCodeButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:verificationCode];
    verificationCode.sd_layout.rightEqualToView(label).heightIs(20).widthIs(70).topSpaceToView(label,3);
    
    //注册
    UIButton * regist = [UIButton new];
    [regist setTitle:@"注    册" forState:0];
    [regist setBackgroundImage:[UIImage imageNamed:@"登录按钮"] forState:0];
    regist.titleLabel.font = [UIFont systemFontOfSize:16];
    [regist setTitleColor:kGetColor(43, 130, 211) forState:0];
    [regist addTarget:self action:@selector(onTapRegistButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regist];
    regist.sd_layout.leftEqualToView(label).rightEqualToView(label).heightIs(40).topSpaceToView(verificationCode,30);
    
}
@end
